package com.nd.android.u.ui.widge;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.common.android.utils.DisplayUtil;
import com.nd.android.u.ui.chatUiUtils.LocalImageManager;

public class ProgressImageView extends ImageView{
	private Paint mPaint=new Paint();
	private String mProgressString;
	private boolean mIsGif=false;
	private int mWidth=0;
	private int mHeight=0;
	public ProgressImageView(Context context) {
		super(context);
		initParam(context);
	}

	public ProgressImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initParam(context);
	}

	
	public ProgressImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initParam(context);
	}
	
	private void initParam(Context context){
		//表示进度的画笔
		mPaint.setColor(0xff222222);
		mPaint.setAntiAlias(true);
		mPaint.setTextSize(DisplayUtil.dip2px(context, 12));
	}
	
	@Override
	public void setImageBitmap(Bitmap bm) {
		super.setImageBitmap(bm);
		//保存图片的长宽，用于绘制文本和GIF图标
		if (bm!=null){
			mWidth=bm.getWidth();
			mHeight=bm.getHeight();
		}
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (mWidth==0 || mHeight==0){
			return;
		}
		if (!TextUtils.isEmpty(mProgressString)){
			//进度文本非空
			int left=(int) (mWidth-mPaint.measureText(mProgressString))/2;
			int top= mHeight/2;
			canvas.drawText(mProgressString, left, top,mPaint);	
		}
		if (mIsGif){
			//是GIF图，要绘制GIF标识
			Bitmap bitmap=LocalImageManager.INSTANCE.getGifFlagBitmap();
			int left=mWidth-bitmap.getWidth();
			int top= mHeight-bitmap.getHeight();
			canvas.drawBitmap(bitmap,left, top, mPaint);
		}
	}
	
	/**
	 * @n<b>函数名称</b>     :setProgress
	 * @brief 设置进度文本
	 * @version    Ver 1.3.3_modularization
	 * @param  @param progress
	 * @return void
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-3-26上午12:22:23      
	*/
	public void setProgress(String progress){
		mProgressString=progress;
		postInvalidate();
	}
	
	/**
	 * @n<b>函数名称</b>     :setIsGif
	 * @brief 设置是否GIF图片
	 * @version    Ver 1.3.3_modularization
	 * @param  @param isGif
	 * @return void
	 * @<b>作者</b>          :  cb
	 * @<b>创建时间</b>      :  2014-3-26上午12:22:33      
	*/
	public void setIsGif(boolean isGif){
		mIsGif=isGif;
		postInvalidate();
	}
}
